<%-- 
	Document   : state_distances
	Created on : Dec 6, 2008, 4:46:07 AM
	Author     : cgrant
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:v="urn:schemas-microsoft-com:vml">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<title>State distances</title>
		<script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=ABQIAAAAOypY6k-8wPRekCAdGgjT5BRvL15kdZZPrZMBY-bNOnix62nrQxTdBreIh1lElRK5kxihCNlVUd4HDg"
		type="text/javascript"> </script>
		<script type="text/javascript">

			var map = null;
			var geocoder = null;
			var point_a = null;
			var point_b = null;
			var zoom_level = 5;

			function initialize() {
				if (GBrowserIsCompatible()) {
					//map = new GMap2(document.getElementById("map_canvas"));
					//map.setMapType(G_HYBRID_MAP);
					//map.setCenter(new GLatLng(37.4419, -122.1419), 13);
					geocoder = new GClientGeocoder();
				}
			}

			function getDistance(from,to){
				var from_point;
				var to_point;

				var from_id = from.replace(" ", "");
				var to_id = to.replace(" ","");
				var the_id = from_id+to_id;

				if(geocoder == null)
					geocoder = new GClientGeocoder();
				//map = new GMap2(document.getElementById("map_canvas"));
				//map.setMapType(G_HYBRID_MAP);
				geocoder.getLatLng(from, function(point){

					from_point = point;
					geocoder.getLatLng(to, function(point){
						//alert("from id"+from_id);
						//alert("to id"+to_id);

						to_point = point;
						//map.setCenter(from_point,13);
						
						var dis = from_point.distanceFrom(to_point);
						//alert(dis);
						var v = document.getElementById(the_id);
						//v.setAttribute("distance", dis);
						v.innerHTML = "insert into statedistance values (\'"+from+"','"+to+"',"+dis+");";S
					});
				});
				//while(window.dis==null){}
				//alert(window.dis);
				//return window.dis;
			}

			function showAddress(address) {
				var a_address = address.split(";");

				for(var i = 0; a_address.length > i; i++){

					if (geocoder) {
						geocoder.getLatLng(
						a_address[i],
						function(point) {
							if (!point) {
								alert(a_address[i] + " not found");
							} else {
								map.setCenter(point, zoom_level);
								var marker = new GMarker(point);
								map.addOverlay(marker);
								// marker.openInfoWindowHtml("The address is: "+a_address[i]);

								point_b = point_a;
								point_a = point;
								if(point_a != null && point_b != null){
									var opts = {clickable:true,geodesic:true};
									var polyline = new GPolyline([point_a,point_b],"#ff0000", 5, .5, opts);
									map.addOverlay(polyline);
								}
							} // else
						} // function
					);
					}   // if(geocoder)
				}  // for loop
			}
		</script>
	</head>
	<body onload="initialize()" onunload="GUnload()">
		<%
		java.sql.Connection conn = null;
		java.sql.ResultSet result = null;
		java.sql.Statement stmt = null;

		conn = flight.DbConnector.getConnection();
		stmt = conn.createStatement();

		final String query = String.format("SELECT FROM_STATE,TO_STATE " +
				"FROM BL1.STATEDISTANCE");

		result = stmt.executeQuery(query);

		while (result.next()) {
			String from = result.getString("FROM_STATE");
			String to = result.getString("TO_STATE");

			String from_nospace = from.replaceAll(" ", "");
			String to_nospace = to.replaceAll(" ", "");


		%>
		<div onmouseout="getDistance('<%=from%>','<%=to%>');" id="<%= from_nospace + to_nospace%>" onmouseover="getDistance('<%=from%>','<%=to%>');" id="<%= from_nospace + to_nospace%>" name="<%= from_nospace + to_nospace%>" distance="">
			|<%= from + to%>|
		</div>
		<%
		}

		result.close();
		stmt.close();
		conn.close();
		
		%>

	</body>
</html>
